pub mod constants;
pub mod error;
pub mod ip_pool;
pub mod network;
pub mod proto;
pub mod utils;
pub mod war3_server;

// 服务器和客户端模块
pub mod server;
pub mod client;

// 重新导出protobuf类型和常用类型
pub use error::War3Error;
pub use proto::*;
pub use war3_server::War3Server;

#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_ip_pool() {
        let pool = crate::ip_pool::IpPool::new();
        let ip1 = pool.acquire_ip().await.unwrap();
        let ip2 = pool.acquire_ip().await.unwrap();
        
        // 验证IP格式正确
        assert!(ip1.starts_with("127.0.1."), "IP should start with 127.0.1.");
        assert!(ip2.starts_with("127.0.1."), "IP should start with 127.0.1.");
        
        // 验证IP都是唯一的
        assert_ne!(ip1, ip2);
    }

    #[test]
    fn test_proto_encoding() {
        // 测试proto编解码
        use crate::proto::{War3Message, MessageType, encode_message, decode_message};

        let message = War3Message {
            r#type: MessageType::ClientConnect as i32,
            source_ip: "127.0.0.1".to_string(),
            target_ip: "127.0.0.2".to_string(),
            payload: vec![1, 2, 3],
        };

        let encoded = encode_message(&message).unwrap();
        let decoded = decode_message(&encoded).unwrap();

        assert_eq!(message, decoded);
    }
}